using System;
using System.Threading;
using SKBKontur.Cassandra.Proxy.Selectors;

namespace SKBKontur.Cassandra.Proxy.Management
{
    public class FactoryStatistics : OperationalStatistics
    {
        public int ActiveStorageCount;
        public int BadStorageCount;
        public int TotalStorageCount;
        public int InUseStorageCount;

        public void ReportBroken()
        {
            Interlocked.Increment(ref BadStorageCount);
            Interlocked.Decrement(ref ActiveStorageCount);
            Interlocked.Decrement(ref InUseStorageCount);
        }

        public void ReportUsageFromPool()
        {
            Interlocked.Increment(ref InUseStorageCount);
        }

        public void ReportRecycle()
        {
            Interlocked.Decrement(ref InUseStorageCount);
        }

        public void ReportUsage()
        {
            Interlocked.Increment(ref TotalStorageCount);
            Interlocked.Increment(ref InUseStorageCount);
            Interlocked.Increment(ref ActiveStorageCount);
        }

        public int GetUsage()
        {
            return TotalStorageCount;
        }

        public bool CanContinue(StorageOperation operation, int counter)
        {
            switch(counter)
            {
                case 0:
                    throw new InvalidOperationException();
                case 1:
                    Thread.Sleep(30);
                    return true;
                case 2:
                    Thread.Sleep(100);
                    return true;
                case 3:
                    Thread.Sleep(1000);
                    return true;
                default:
                    return false;
            }
        }
    }
}